package com.lvwenguo.controller.filter;







import com.sun.org.apache.bcel.internal.classfile.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class indexFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse= (HttpServletResponse) servletResponse;

        String uri = httpServletRequest.getRequestURI();

        httpServletResponse.setContentType("text/html;charset=UTF-8");

        if(httpServletRequest.getSession().getAttribute("sessionname")!=null ){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else{
            if(uri.endsWith("login") || uri.endsWith("login.jsp")){
                filterChain.doFilter(servletRequest,servletResponse);
            }else{
                httpServletResponse.sendRedirect("http://localhost:8080/supermarket/login.jsp");
            }

        }
    }

    @Override
    public void destroy() {

    }
}
